home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / world / depth / depth.java < prev    next >
Text File  |  1996-10-17  |  3KB  |  203 lines

  1. // "Motion Shadow Effect" with depth of shadow
  2.  
  3. //     created by ask@krc.sony.co.jp (Masamichi zzzcat Asukai)
  4.  
  5. //
  6.  
  7. // Copyright(C) 1996 Sony Corporation. All rights reserved.
  8.  
  9. //
  10.  
  11.  
  12.  
  13. import vrml.*;
  14.  
  15. import vrml.node.*;
  16.  
  17. import vrml.field.*;
  18.  
  19.  
  20.  
  21. public class depth extends Script {
  22.  
  23.  
  24.  
  25.     private SFRotation setRotation;
  26.  
  27.     private SFVec3f setScale;
  28.  
  29.     private SFFloat setTransparency;
  30.  
  31.  
  32.  
  33.     private Node tree;
  34.  
  35.     private Node light;
  36.  
  37.  
  38.  
  39.     private float[] s_rot = {0.0f, 1.0f, 0.0f, 0.0f};
  40.  
  41.     private float[] s_scale = {1.0f, 1.0f, 1.0f};
  42.  
  43.     private float s_transparency;
  44.  
  45.  
  46.  
  47.     private float[] t_trans = new float[3];
  48.  
  49.     private float[] t_scale = new float[3];
  50.  
  51.     private float[] l_trans = new float[3];
  52.  
  53.  
  54.  
  55.     private float[] vec = new float[3];
  56.  
  57.     private float d;
  58.  
  59.  
  60.  
  61.     // constructor
  62.  
  63.     public void initialize() {
  64.  
  65.     setRotation = (SFRotation)getEventOut("setRotation");
  66.  
  67.     setScale = (SFVec3f)getEventOut("setScale");
  68.  
  69.     setTransparency = (SFFloat)getEventOut("setTransparency");
  70.  
  71.  
  72.  
  73.     tree = (Node)((SFNode)getField("tree")).getValue();
  74.  
  75.     light = (Node)((SFNode)getField("light")).getValue();
  76.  
  77.  
  78.  
  79.     // get translation and scale of tree
  80.  
  81.     ((SFVec3f)tree.getExposedField("translation")).getValue(t_trans);
  82.  
  83.     ((SFVec3f)tree.getExposedField("scale")).getValue(t_scale);
  84.  
  85.     }
  86.  
  87.     
  88.  
  89.     public void processEvent(Event e) {
  90.  
  91.     if (e.getName().equals("interval")) {
  92.  
  93.         // get translation of light
  94.  
  95.         ((SFVec3f)light.getExposedField("translation")).getValue(l_trans);
  96.  
  97.  
  98.  
  99.         ////////////////////
  100.  
  101.         // SHADOW ROTATION
  102.  
  103.         ////////////////////
  104.  
  105.  
  106.  
  107.         // normalized light vector on X-Z plane
  108.  
  109.         vec[0] = t_trans[0] - l_trans[0];
  110.  
  111.         vec[2] = t_trans[2] - l_trans[2];
  112.  
  113.         d = (float)java.lang.Math.sqrt(vec[0]*vec[0] + vec[2]*vec[2]);
  114.  
  115.         vec[0] /= d;
  116.  
  117.         vec[2] /= d;
  118.  
  119.  
  120.  
  121.         // rotation of shadow
  122.  
  123.         if (vec[0] < 0.0) {
  124.  
  125.             s_rot[3] = (float)java.lang.Math.acos(-vec[2]);
  126.  
  127.         } else {
  128.  
  129.             s_rot[3] = -(float)java.lang.Math.acos(-vec[2]);
  130.  
  131.         }
  132.  
  133.  
  134.  
  135.         // set rotation of shadow
  136.  
  137.             setRotation.setValue(s_rot);
  138.  
  139.  
  140.  
  141.         ////////////////////
  142.  
  143.         // SHADOW LENGTH
  144.  
  145.         ////////////////////
  146.  
  147.  
  148.  
  149.         // whether light height is higher than top of tree
  150.  
  151.         if (l_trans[1] < t_scale[1]) {
  152.  
  153.             s_scale[2] = 0.0f;
  154.  
  155.         } else {
  156.  
  157.             s_scale[2] = d / (l_trans[1] - t_scale[1]);
  158.  
  159.         }
  160.  
  161.  
  162.  
  163.         // set length of shadow
  164.  
  165.             setScale.setValue(s_scale);
  166.  
  167.  
  168.  
  169.         ////////////////////
  170.  
  171.         // SHADOW DEPTH
  172.  
  173.         ////////////////////
  174.  
  175.  
  176.  
  177.         if (s_scale[2] < 1.0f) {
  178.  
  179.             s_transparency = 0.0f;
  180.  
  181.         } else {
  182.  
  183.             s_transparency = 1.0f / s_scale[2];
  184.  
  185. //            s_transparency = 1.0f / (s_scale[2] * s_scale[2]);
  186.  
  187.             s_transparency = 1.0f - s_transparency;
  188.  
  189.         }
  190.  
  191.  
  192.  
  193.         // set depth of shadow
  194.  
  195.             setTransparency.setValue(s_transparency);
  196.  
  197.         }
  198.  
  199.     }
  200.  
  201. }
  202.  
  203.